<?php
namespace Admin\Controller;

class IndexController extends CommonController {
    public function index()
    {
        /* @var $admin_auth_group_access_model \Admin\Model\AdminAuthGroupAccessModel */
        $user_info = session('user_info');
        $admin_auth_group_access_model = D('AdminAuthGroupAccess');
        $menus = $admin_auth_group_access_model->getUserRules($user_info['id']);
        $this->assign('menus', $menus);
        $this->display();
    }
    public function edit(){
        if(IS_POST){
            $userName = I('post.user_name', '', 'string');
            $realName = I('post.real_name', '', 'string');
            $passWord = I('post.pass_word', '', 'string');
            $newWord = I('post.new_pass_word', '', 'string');
            if(empty($realName)){
                $this->ajaxError("用户真实姓名不能为空！");
            }
            $data['real_name'] = $realName;
            if(!empty($passWord)){
                if(empty($newWord) || strlen($newWord)<6){
                    $this->ajaxError("新密码不能为空且长度大于6位");
                }
                $data['password'] = md5($newWord);
                $passWord = md5($passWord);
                $ret = M('auth_user')->where("user_name = '$userName' and password = '$passWord' ")->save($data);
            }else{
                $ret = M('auth_user')->where("user_name = '$userName'")->save($data);
            }
            if ($ret>0) {
                $this->ajaxSuccess("修改成功");
            } else {
                $this->ajaxError("旧密码错误，修改失败或无更新内容");
            }
        }else{
            $info = session('user_info');
            $this->assign('info', $info);
            $this->display();
        }
    }

    public function nav()
    {
        $this->display();
    }


    public function login()
    {
       $this->display();
    }

    public function form()
    {
        $this->display();
    }


    public function table()
    {
       $this->display();

    }

    public function main()
    {
        $list = M('git_update')->limit(5)->order('id desc')->select();
        $this->assign('list',$list);
        $this->display();
    }
    public function upload()
    {
        if(IS_POST){
           $img = $_FILES['file'];
           $upload = new \Think\Upload();// 实例化上传类
           $upload->maxSize   = 3145728 ;// 设置附件上传大小
           $upload->exts      = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
           $upload->rootPath  = './Public/'; // 设置附件上传根目录
           $upload->savePath  = 'upload/'; // 设置附件上传（子）目录
           // 上传文件
           $info   =   $upload->uploadOne($img);


           if(!$info) {// 上传错误提示错误信息
               echo json_encode(array('status' => 'error','msg' => $upload->getError()));
               exit;
           }else{// 上传成功

               $imgpath = $info['savepath'].$info['savename'];
               echo json_encode(array('status' => 'success','url'=>'/Public/'.$imgpath));
               exit;
           }

        }else{
            $this->display();
        }

    }
    //验证码
    public function verify(){
        $Verify = new \Think\Verify();
        $Verify->codeSet = '0123456789';// 设置验证码字符为纯数字
        $Verify->length = 4;
        $Verify->imageH = 37;
        $Verify->imageW = 120;
        $Verify->fontSize = 18;
        $Verify->useNoise = true;
        $Verify->useCurve = true;
        $Verify->fontttf = "5.ttf";
        $Verify->bg = array(196,223,246);
        $Verify->entry();
    }
}
